package com.example.zg.androidnewview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zg.androidnewview.adapter.BaseHFRecyclerViewAdapter;
import com.example.zg.androidnewview.adapter.BaseRecycleViewAdapter;
import com.example.zg.androidnewview.animator.BaseItemAnimator;
import com.example.zg.androidnewview.customView.RectDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
    private static final String TAG = "RecyclerViewActivity";
    private RecyclerView recyclerView1;
    private BaseHFRecyclerViewAdapter<String> baseRecycleViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        init();
    }

    private List<String> initData(){
        List<String> list=new ArrayList<>();
        for(int i=0;i<30;i++){
            list.add("item"+i);
        }
        return list;
    }

    private void init(){
        recyclerView1=findViewById(R.id.recyclerView1);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,4);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);

        recyclerView1.setLayoutManager(linearLayoutManager);
        //设置Adapter
        baseRecycleViewAdapter = new BaseHFRecyclerViewAdapter<String>(initData()) {

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item2;
            }

            @Override
            public void onBindData(VH holder, String data, int position, int viewType) {
                ImageView imageView = holder.getView(R.id.image);
                imageView.setImageResource(R.drawable.daohang);
                holder.setText(R.id.text_value, data);
            }
        };

        baseRecycleViewAdapter.setOnItemListener(new BaseHFRecyclerViewAdapter.OnItemListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Toast.makeText(RecyclerViewActivity.this, position+" : "+baseRecycleViewAdapter.getItemData(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClickListener(View view, final int position) {
                return true;
            }
        });
        View header=getLayoutInflater().inflate(R.layout.header,null,false);
        View footer=getLayoutInflater().inflate(R.layout.footer,null,false);
        baseRecycleViewAdapter.setHeader(header);
        baseRecycleViewAdapter.setFooter(footer);

        recyclerView1.setAdapter(baseRecycleViewAdapter);
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(new ItemTouchHelper.Callback() {
            //设置支持的拖拽、滑动的方向
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; //s上下拖拽
                int swipeFlag = ItemTouchHelper.START | ItemTouchHelper.END; //左->右和右->左滑动
                return makeMovementFlags(dragFlag,swipeFlag);
            }

            //拖拽时回调。
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                //交换位置
                int from = viewHolder.getAdapterPosition();
                int to = target.getAdapterPosition();
                Collections.swap(baseRecycleViewAdapter.getData(), from, to);
                baseRecycleViewAdapter.notifyItemMoved(from, to);
                Log.i(TAG, "onMove: ");
                return true;
            }

            //滑动时回调。
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int pos = viewHolder.getAdapterPosition();
                baseRecycleViewAdapter.getData().remove(pos);
                baseRecycleViewAdapter.notifyItemRemoved(pos);
                Log.i(TAG, "onSwiped: direction="+direction);
            }
        });
        itemTouchHelper.attachToRecyclerView(recyclerView1);

       /* recyclerView1.setAdapter(new BaseRecycleViewAdapter<String>(initData()){

            @Override
            public int getItemViewType(int position) {
                //第奇数项和偶数项用不同的布局文件,这里返回不同的viewType
                if(position%2==0){
                    return 0;
                }else{
                    return 1;
                }
            }

            @Override
            public int getLayoutId(int viewType) {
                //根据viewType使用不同的布局文件
                if(viewType==0){
                    return R.layout.item1;
                }else{
                    return R.layout.item2;
                }
            }

            @Override
            public void onBindData(VH holder,String data, int position, int viewType) {
                //给不同的布局文件绑定数据
                switch (viewType){
                    case 0:
                        holder.setText(R.id.text_value1,data);
                        break;
                    case 1:
                        holder.setText(R.id.text_value,data);
                        ImageView imageView=holder.getView(R.id.image);
                        imageView.setImageResource(R.drawable.daohang);
                        break;
                }
            }
        });*/


        //设置分割线
//        recyclerView1.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
//        recyclerView1.addItemDecoration(new RectDecoration().setStrokeWidth(2).setItemInterval(0).setDivideColor(Color.RED));
        //设置增加或删除条目的动画

        recyclerView1.setItemAnimator(new DefaultItemAnimator());

    }


    public void addItems(View view) {
        List<String> data = baseRecycleViewAdapter.getData();
        data.add(2,"新增"+data.size());
        baseRecycleViewAdapter.notifyItemInserted(2);
    }

    public void deleteItems(View view) {
        List<String> data = baseRecycleViewAdapter.getData();
        data.remove(2);
        baseRecycleViewAdapter.notifyItemRemoved(2);
    }
}
